package com.tloss.stdao.service;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.tloss.Constants;
import com.tloss.stdao.JPAResourceBean;
import com.tloss.stdao.Profit;
import com.tloss.stdao.StockHistory;

public class ProfitService implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4531565877284007518L;

	/**
	 * Unsupported Features of JPA The following features of the JPA interface
	 * are not supported by the App Engine implementation:<br/>
	 * <li>Owned many-to-many relationships, and unowned relationships. You can
	 * implement unowned relationships using explicit Key values, though type
	 * checking is not enforced in the API.<br/> <li>"Join" queries. You cannot
	 * use a field of a child entity in a filter when performing a query on the
	 * parent kind. Note that you can test the parent's relationship field
	 * directly in a query using a key.<br/> <li>Aggregation queries (group by,
	 * having, sum, avg, max, min)<br/> <li>Polymorphic queries. You cannot
	 * perform a query of a class to get instances of a subclass. Each class is
	 * represented by a separate entity kind in the datastore.
	 */
	public Profit select(String username) {
		Profit profit = new Profit();
		EntityManager em = JPAResourceBean.getEMF().createEntityManager();
		Query q = em
				.createQuery("select t from StockHistory t where t.username=?1 and t.transactionType=?2");
		q.setParameter(1, username);
		q.setParameter(2, Constants.BUY);
		List<StockHistory> histories = q.getResultList();
		int sumBuy = 0;
		for (StockHistory stockHistory : histories) {
			sumBuy = sumBuy + stockHistory.getValue()
					* stockHistory.getQuality();
		}
		q = em.createQuery("select t from StockHistory t where t.username=?1 and t.transactionType=?2");
		q.setParameter(1, username);
		q.setParameter(2, Constants.SELL);
		int sumSell = 0;
		histories = q.getResultList();
		for (StockHistory stockHistory : histories) {
			sumSell = sumSell + stockHistory.getValue()
					* stockHistory.getQuality();
		}
		profit.setSumBuy(sumBuy);
		profit.setSumSell(sumSell);
		profit.setTotal(sumSell - sumBuy);
		em.close();
		return profit;
	}

	public Profit selectSupportSum(String username) {
		Profit profit = new Profit();
		EntityManager em = JPAResourceBean.getEMF().createEntityManager();
		Query q = em
				.createQuery("select SUM(t.value*t.quality) from StockHistory t where t.username=?1 and t.transactionType=?2");
		q.setParameter(1, username);
		q.setParameter(2, Constants.BUY);
		int sumBuy = ((Number) q.getSingleResult()).intValue();
		q = em.createQuery("select SUM(t.value*t.quality) from StockHistory t where t.username=?1 and t.transactionType=?2");
		q.setParameter(1, username);
		q.setParameter(2, Constants.SELL);
		int sumSell = ((Number) q.getSingleResult()).intValue();
		profit.setSumBuy(sumBuy);
		profit.setSumSell(sumSell);
		profit.setTotal(sumSell - sumBuy);
		em.close();
		return profit;
	}
}
